declare let eManager;
import DrawBase from "./DrawBase";
const {ccclass, property} = cc._decorator;
/*
2-3 刷阴影
2-4 刷高光
3-2 腮红
*/
@ccclass
export default class GaoGuang extends DrawBase {
    @property
    stepName = "";
    @property
    iconTipsName = "";
    //刷子入场动画 
    @property(cc.Animation)
    startAni: cc.Animation = null;

    //真刷子 
    @property(cc.Node)
    shuaZi: cc.Node = null;

    //真刷子spine
    @property(sp.Skeleton)
    shuaZiSk: sp.Skeleton = null;

    //假刷子spine
    @property(sp.Skeleton)
    shuaZiSk2: sp.Skeleton = null;

    //完成后的效果图
    @property(cc.Node)
    doneEffect:cc.Node = null;

    //刷子刷动音效
    @property({type:cc.AudioClip})
    shuaziSound: cc.AudioClip = null;

    //是否有可选列表
    @property
    isSelectList = false;

    _isPlay = false;
    _audioId = 0;
    _isDone = false;
    _offset = cc.v2(0,0);
    _curPoint = "";
    start(){
        eManager.emit("REFRESH_STEP_NAME", this.stepName);
        eManager.emit("HZ_REFRESH_ICON_TIPS", (this.iconTipsName && this.iconTipsName.length > 0) ? this.iconTipsName : null);
        this.startAni.on("finished",this.playFinish,this);
    };
    initData(){
        //@ts-ignore
        this.graphics = this.mask._graphics;  
        this.reset();
        this.generateKeyPoints();
        // if(!this.isSelectList){
            this._startPlay();
        // }
        // if(this.isSelectList){
        //     eManager.emit("HZ_SHOW_SELECT_LIST", {
        //         step: 3,
        //         point: 2,
        //         // callback:()=>{
        //         //     this._startPlay();
        //         // }
        //     });
        // }
        eManager.emit("REFRESH_STEP_NAME", this.stepName);
    };

    _startPlay(){
        this.shuaZi.active = false;
        this.mask.node.active = true;
        this.doneEffect.active = false;
        this.startAni.node.active = true;
        this.startAni.play();
        this._isPlay = true;
        this.scheduleOnce(() => {
            this.shuaZiSk2.setAnimation(0, "animation", false);
        }, 1);
    };

    _touchStart(event:any,curPoint:string){
        this._curPoint = curPoint;
        if(this.shuaZiSk){
            this.shuaZiSk.setAnimation(0,"animation",true);
            if (this._audioId > 0) {
                cc.audioEngine.stop(this._audioId);
            }
            let worldPoint = event.touch.getLocation();
            let localPoint = this.shuaZi.parent.convertToNodeSpaceAR(worldPoint);
            if(curPoint == "yinying"){
                this._offset = cc.v2(-150,150);
            }else if(curPoint = "saihong"){
                this._offset = cc.v2(-200,-200);
            }
            this.shuaZi.x = localPoint.x + this._offset.x;
            this.shuaZi.y = localPoint.y + this._offset.y;
            this._audioId =  cc.audioEngine.play(this.shuaziSound, true, 1);
            this._lastLoc = cc.v2(this.shuaZi.x, this.shuaZi.y);
        }
    };
    _touchMove(event){
        if(this._isPlay)
            return;
        if(this._isDone)
            return;
        // let delta = event.touch.getDelta();
        // this.shuaZi.x += delta.x;
        // this.shuaZi.y += delta.y;
        let worldPoint = event.touch.getLocation();
        let localPoint = this.shuaZi.parent.convertToNodeSpaceAR(worldPoint);
        if (this._curPoint == "yinying") {
            this._offset = cc.v2(-150, 150);
        } else if (this._curPoint = "saihong") {
            this._offset = cc.v2(-150, -150);
        }
        this.shuaZi.x = localPoint.x + this._offset.x;
        this.shuaZi.y = localPoint.y + this._offset.y;
        this.drawLine(cc.v2(this.shuaZi.x, this.shuaZi.y));
    };

    _touchEnd(event:any,curPoint:string){
        cc.audioEngine.stop(this._audioId);
        this._audioId = 0;
        if(this._isDone)
            return;
        this.shuaZiSk.setAnimation(0,"moren",true);
        let done = this.checkKeyPoints();
        //抹完高光
        if (done) {
            this.shuaZi.active = false;
            this.mask.node.active = false;
            this.doneEffect.active = true;
            if (curPoint == "yinying") {
                eManager.emit("DONE_STEP", 2);
                eManager.emit("MINI_DONE_POINT", 1);
                eManager.emit("HZ_MAKE_DOT", {
                    makeup_close:3,
                    makeup_step:this.stepName
                });
                this._isDone = true;
            // } else if(curPoint == "gaoguang"){
            //     eManager.emit("DONE_STEP", 2);
            //     eManager.emit("HZ_MAKE_DOT", {
            //         makeup_close:2,
            //         makeup_step:this.stepName
            //     });
            //     this._isDone = true;
            }else if(curPoint == "saihong"){
                eManager.emit("DONE_STEP", 3);
                eManager.emit("MINI_DONE_POINT", 3);
                eManager.emit("HZ_MAKE_DOT", {
                    makeup_close:3,
                    makeup_step:this.stepName
                });
                this._isDone = true;
            }
        }
    };

    playFinish(){
        this._isPlay = false;
        this.shuaZi.active = true;
    };


}
